package com.weijianhuawen.wjblog.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleCategoryRelDo;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleDo;
import com.weijianhuawen.wjblog.common.domain.dos.CategoryDo;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.CategoryMapper;
import com.weijianhuawen.wjblog.common.enums.ResponseCodeEnum;
import com.weijianhuawen.wjblog.common.exception.BizException;
import com.weijianhuawen.wjblog.common.utils.PageResponse;
import com.weijianhuawen.wjblog.common.utils.Response;
import com.weijianhuawen.wjblog.web.convert.ArticleConvert;
import com.weijianhuawen.wjblog.web.mode.vo.category.FindCategoryArticlePageListReqVo;
import com.weijianhuawen.wjblog.web.mode.vo.category.FindCategoryArticlePageListRspVo;
import com.weijianhuawen.wjblog.web.mode.vo.category.FindCategoryListReqVo;
import com.weijianhuawen.wjblog.web.mode.vo.category.FindCategoryListRspVo;
import com.weijianhuawen.wjblog.web.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-28 22:54
 */

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ArticleCategoryRelMapper articleCategoryRelMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Override
    public Response findCategoryList(FindCategoryListReqVo vo) {
        // 查询分类
        List<CategoryDo> categoryDos = Lists.newArrayList();
        if (Objects.isNull(vo) || Objects.isNull(vo.getSize()) || vo.getSize() <= 0) {
            // 参数为空，查询所有分类
            categoryDos = categoryMapper.selectList(Wrappers.emptyWrapper());
        } else {
            categoryDos = categoryMapper.selectByLimit(vo.getSize());
        }

        // 将do 转 vo
        List<FindCategoryListRspVo> findCategoryListRspVos = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(categoryDos)) {
            categoryDos.forEach(categoryDo -> {
                FindCategoryListRspVo findCategoryListRspVo = FindCategoryListRspVo.builder()
                        .id(categoryDo.getId())
                        .name(categoryDo.getName())
                        .articlesTotal(categoryDo.getArticlesTotal())
                        .build();

                findCategoryListRspVos.add(findCategoryListRspVo);
            });
        }

        return Response.success(findCategoryListRspVos);
    }

    @Override
    public PageResponse findCategoryArticlePageList(FindCategoryArticlePageListReqVo vo) {
        // 获取分类id以及分页查询条件
        Long categoryId = vo.getId();
        // 获取分页查询页码
        Long current = vo.getCurrent();
        // 获取每一页查询数据
        Long size = vo.getSize();

        // 查找分类，是否存在该分类id对应的分类
        CategoryDo categoryDo = categoryMapper.selectById(categoryId);

        // 分类不存在， 返回抛出异常
        if (Objects.isNull(categoryDo)) {
            log.warn("【前台分类服务】所查询分类id对应分类不存在");
            throw new BizException(ResponseCodeEnum.NOT_EXIST_CATEGORY);
        }

        // 查询该分类所关联的所有文章关联记录
        List<ArticleCategoryRelDo> articleCategoryRelDos = articleCategoryRelMapper.selectListByCategory(categoryId);

        // 不存在关联记录
        if (CollectionUtils.isEmpty(articleCategoryRelDos)) {
            log.info("【前台分类服务】 该分类：{}未关联任何文章", categoryDo.getName());
            return PageResponse.success(null, null);
        }

        // 获取分类id所关联所有列表的文章id
        List<Long> articleIds = articleCategoryRelDos.stream().map(ArticleCategoryRelDo::getArticleId).collect(Collectors.toList());

        // 根据文章id集合查询文章信息
        Page<ArticleDo> page = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        // 获取数据记录
        List<ArticleDo> articleDoList = page.getRecords();

        // 将do 转 vo
        List<FindCategoryArticlePageListRspVo> vos = Lists.newArrayList();

        if (!CollectionUtils.isEmpty(articleDoList)) {
            vos = articleDoList.stream()
                    .map(articleDo -> ArticleConvert.INSTENCE.convertDoToCategoryArticleVo(articleDo))
                    .collect(Collectors.toList());
        }
        return PageResponse.success(page, vos);
    }
}
